#include "DirOpt.h"
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
using namespace boost;

CDirOpt::CDirOpt(void)
{
}

CDirOpt::~CDirOpt(void)
{
}

bool CDirOpt::exists( std::string dir_path )
{
  try
  {
    return boost::filesystem::exists(dir_path);
  }
  catch(...)
  {
    return false;
  }
}

bool CDirOpt::create_directories( std::string dir_path )
{
  try
  {
    return boost::filesystem::create_directories(dir_path);
  }
  catch(...)
  {
    return false;
  }
}

bool CDirOpt::is_empty( std::string dir_path )
{
  try
  {
    return boost::filesystem::is_empty(dir_path);
  }
  catch(...)
  {
    return false;
  }
}

bool CDirOpt::remove_all( std::string dir_path )
{
  try
  {
    boost::uintmax_t ret = boost::filesystem::remove_all(dir_path);
    return true;
  }
  catch(...)
  {
    return false;
  }
}

bool CDirOpt::scan_directory( const std::string& dir, ARR_STRING& arrFiles, bool recursive /*= true*/ )
{
  if(!is_directory(dir))
    return false;

  recursive_dir(dir, arrFiles, recursive);
  return true;
}

void CDirOpt::recursive_dir( const boost::filesystem::path& dir, ARR_STRING& arrFiles, bool recursive /*= true*/ )
{
  directory_iterator end;
  
  for(boost::filesystem::directory_iterator pos(dir); pos != end; ++pos)
  {
    if(is_directory(*pos))
    {
      if(recursive)
        recursive_dir( (*pos), arrFiles, recursive );
    }
    else
    {
      arrFiles.push_back( path(*pos).string() );
    }
  }
}
